package com.sqlcheck.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sqlcheck.common.enums.DatabaseType;
import com.sqlcheck.entity.LLMOptimizationRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface LLMOptimizationRecordRepository extends BaseMapper<LLMOptimizationRecord> {

    /**
     * 根据任务ID查询优化记录
     */
    @Select("SELECT * FROM llm_optimization_records WHERE task_id = #{taskId} AND deleted = 0 ORDER BY created_at DESC")
    List<LLMOptimizationRecord> findByTaskId(@Param("taskId") Long taskId);

    /**
     * 根据用户ID查询优化记录
     */
    @Select("SELECT * FROM llm_optimization_records WHERE user_id = #{userId} AND deleted = 0 ORDER BY created_at DESC LIMIT #{limit}")
    List<LLMOptimizationRecord> findByUserId(@Param("userId") Long userId, @Param("limit") Integer limit);

    /**
     * 查询指定时间范围内的优化记录
     */
    @Select("SELECT * FROM llm_optimization_records WHERE created_at BETWEEN #{startTime} AND #{endTime} AND deleted = 0")
    List<LLMOptimizationRecord> findByTimeRange(@Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);

    /**
     * 根据数据库类型查询优化记录统计
     */
    @Select("SELECT COUNT(*) FROM llm_optimization_records WHERE source_db_type = #{sourceDbType} AND target_db_type = #{targetDbType} AND deleted = 0")
    Integer countByDatabaseTypes(@Param("sourceDbType") DatabaseType sourceDbType, @Param("targetDbType") DatabaseType targetDbType);

    /**
     * 查询成功的优化记录数量
     */
    @Select("SELECT COUNT(*) FROM llm_optimization_records WHERE status = 'SUCCESS' AND deleted = 0")
    Integer countSuccessfulOptimizations();
}